package com.example.ymm.modules1.controller.sys;

import com.alibaba.fastjson.JSONObject;
import com.example.ymm.dto.Constants;
import com.example.ymm.model.CommonResult;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.controller.BaseController;
import com.example.ymm.modules1.entity.my.user.*;
import com.example.ymm.modules1.query.DbCategoryQueryCriteria;
import com.example.ymm.modules1.query.DbGoodQueryCriteria;
import com.example.ymm.modules1.query.DbOrderItemQueryCriteria;
import com.example.ymm.modules1.query.DbOrderQueryCriteria;
import com.example.ymm.modules1.service.dto.SysRankingQueryCriteria;
import com.example.ymm.modules1.service.user.DbCategoryService;
import com.example.ymm.modules1.service.user.DbGoodService;
import com.example.ymm.modules1.service.user.DbOrderItemService;
import com.example.ymm.modules1.service.user.DbOrderService;
import com.example.ymm.util.UtilValidate;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/hl/sys/good")
@Tag(name= "pc 商品管理")
@Slf4j
@CrossOrigin
public class SysGoodController extends BaseController {

    @Autowired
    private DbGoodService dbGoodService;

    @Autowired
    DbCategoryService dbCategoryService;

    @Autowired
    DbOrderService dbOrderService;

    @Autowired
    DbOrderItemService dbOrderItemService;

    @Operation(summary= "商品列表")
    @RequestMapping(value = "/goodList", method = RequestMethod.POST)
    public CommonResult goodList(@Valid DbGoodQueryCriteria criteria, YmPageDto ymPage) {
        try{
            TableList<DbGood> list = dbGoodService.list(criteria, ymPage);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }




    @Operation(summary= "商品保存")
    @RequestMapping(value = "/saveGood", method = RequestMethod.POST)
    public CommonResult saveGood(DbGood dbGood) {
        try{

            if(UtilValidate.isNotEmpty(dbGood.getCategoryId())){
                DbCategory category = dbCategoryService.getById(dbGood.getCategoryId());
                if(UtilValidate.isNotEmpty(category)){
                    dbGood.setCategoryName(category.getCategoryName());
                }
            }

            dbGoodService.saveOrUpdate(dbGood);

            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Operation(summary= "商品详情")
    @RequestMapping(value = "/goodDetail", method = RequestMethod.POST)
    public CommonResult goodDetail(@Parameter(name = "数据主键") @RequestParam(required = true) String goodId) {
        try{
            DbGood good = dbGoodService.getById(goodId);
            return CommonResult.success(good);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Operation(summary= "商品删除")
    @RequestMapping(value = "/goodDelete", method = RequestMethod.POST)
    public CommonResult goodDelete(@Parameter(name = "数据主键") @RequestParam(required = true) String goodId) {
        try{
            dbGoodService.removeById(goodId);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Operation(summary= "商品上架")
    @RequestMapping(value = "/goodUp", method = RequestMethod.POST)
    public CommonResult goodUp(@Parameter(name = "数据主键") @RequestParam(required = true) String goodId) {
        try{
            DbGood good = dbGoodService.getById(goodId);
            good.setStatus(Constants.STATE_UP);
            dbGoodService.saveOrUpdate(good);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }
    @Operation(summary= "商品下架")
    @RequestMapping(value = "/goodDown", method = RequestMethod.POST)
    public CommonResult goodDown(@Parameter(name = "数据主键") @RequestParam(required = true) String goodId) {
        try{
            DbGood good = dbGoodService.getById(goodId);
            good.setStatus(Constants.STATE_DOWN);
            dbGoodService.saveOrUpdate(good);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Operation(summary= "分类列表")
    @RequestMapping(value = "/categoryList", method = RequestMethod.POST)
    public CommonResult categoryList(@Valid DbCategoryQueryCriteria criteria, YmPageDto ymPage) {
        try{
            TableList<DbCategory> list = dbCategoryService.list(criteria, ymPage);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }




    @Operation(summary= "分类保存")
    @RequestMapping(value = "/saveCategory", method = RequestMethod.POST)
    public CommonResult saveCategory(DbCategory dbCategory) {
        try{
            dbCategoryService.saveOrUpdate(dbCategory);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Operation(summary= "分类详情")
    @RequestMapping(value = "/categoryDetail", method = RequestMethod.POST)
    public CommonResult categoryDetail(@Parameter(name = "数据主键") @RequestParam(required = true) String categoryId) {
        try{
            DbCategory category = dbCategoryService.getById(categoryId);
            return CommonResult.success(category);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Operation(summary= "分类删除")
    @RequestMapping(value = "/categoryDelete", method = RequestMethod.POST)
    public CommonResult categoryDelete(@Parameter(name = "数据主键") @RequestParam(required = true) String categoryId) {
        try{
            dbCategoryService.removeById(categoryId);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Operation(summary= "所有分类")
    @RequestMapping(value = "/categoryAll", method = RequestMethod.POST)
    public CommonResult categoryAll(DbCategoryQueryCriteria criteria) {
        try{
            List<DbCategory> dbCategories = dbCategoryService.queryAll(criteria);
            return CommonResult.success(dbCategories);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Operation(summary= "订单列表")
    @RequestMapping(value = "/orderList", method = RequestMethod.POST)
    public CommonResult orderList(@Valid DbOrderQueryCriteria criteria, YmPageDto ymPage) {
        try{
            TableList<DbOrder> list = dbOrderService.list(criteria, ymPage);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Operation(summary= "订单子项列表")
    @RequestMapping(value = "/orderItemList", method = RequestMethod.POST)
    public CommonResult orderItemList(@Valid DbOrderItemQueryCriteria criteria, YmPageDto ymPage) {
        try{
            TableList<DbOrderItem> list = dbOrderItemService.list(criteria, ymPage);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Operation(summary= "订单取消")
    @RequestMapping(value = "/orderCancel", method = RequestMethod.POST)
    public CommonResult orderCancel(String orderId) {
        try{
            DbOrder dbOrder = dbOrderService.orderCancel(orderId);
            dbOrder.setNote("管理员操作取消订单");
            dbOrderService.saveOrUpdate(dbOrder);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Operation(summary= "取货")
    @RequestMapping(value = "/takesOrder", method = RequestMethod.POST)
    public CommonResult takesOrder(String orderId) {
        try{
            DbOrder dbOrder = dbOrderService.takesOrder(orderId);
            dbOrder.setNote("管理员操作取货");
            dbOrderService.saveOrUpdate(dbOrder);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Operation(summary= "统计系统中 已兑换积分")
    @PostMapping("/totalOrderIntegral")
    public CommonResult totalOrderIntegral() {
        try{
            SysRankingQueryCriteria criteria=new SysRankingQueryCriteria();
            Double b = dbOrderService.totalConversionIntegral(criteria);
            JSONObject json=new JSONObject();
            json.put("totalIntegral",b);
            return CommonResult.success(json);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

}
